package com.qpx.studentservice.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.qpx.studentservice.dto.SutdentDto;
import com.qpx.studentservice.entity.THouse;
import com.qpx.studentservice.entity.TStudent;
import com.qpx.studentservice.mapper.TStudentMapper;
import com.qpx.studentservice.service.ITHouseService;
import com.qpx.studentservice.service.ITStudentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-02-10
 */
@Service
public class TStudentServiceImpl extends ServiceImpl<TStudentMapper, TStudent> implements ITStudentService {

    @Autowired
    ITHouseService itHouseService;
    @Override
    public List<TStudent> selectStudentList(TStudent tStudent) {
        return this.baseMapper.selectStudentList(tStudent);
    }

    @Override
    @Transactional
    public void save(SutdentDto sutdentDto,TStudent tStudent1) {
        //插入t_house 表  先删除后插入
        Wrapper<THouse> wrapper = new EntityWrapper<THouse>();
        wrapper.eq("stu_id",tStudent1.getId());
        itHouseService.delete(wrapper);

        for (THouse tHouse : sutdentDto.getHouseList()) {
            tHouse.setStuId(tStudent1.getId());
        }
        itHouseService.insertBatch(sutdentDto.getHouseList());
    }

    @Transactional
    public void delete(Integer id){
        this.deleteById(id);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("stu_id",id);
        itHouseService.delete(wrapper);
    }
}
